package com.ypj.indusynergyems.config;

import com.ypj.indusynergyems.common.biz.user.SystemAuthorityInterceptor;
import com.ypj.indusynergyems.common.biz.user.UserTransmitFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 用户配置自动装配
 */
@Configuration
public class UserConfiguration implements WebMvcConfigurer{
    /**
     * 用户信息传递过滤器
     */
    //@Bean
    public FilterRegistrationBean<UserTransmitFilter> globalUserTransmitFilter(StringRedisTemplate stringRedisTemplate) {
        FilterRegistrationBean<UserTransmitFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new UserTransmitFilter(stringRedisTemplate));
        registration.addUrlPatterns("/*");
//        registration.addInitParameter("Login","/system/login");
//        registration.setOrder(USER_TRANSMIT_FILTER_ORDER);
        registration.setOrder(0);
        return registration;
    }

    //配置统一视图跳转
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //addViewController:浏览器访问路径(http://localhost:8080/login.html)
        //setViewName:实际访问的路径,要给指定的名称添加前缀和后缀(templates/admin/system/login.html
        registry.addViewController("header.html").setViewName("/system/common/header");
        registry.addViewController("left.html").setViewName("/system/common/left");
        registry.addViewController("login.html").setViewName("/system/login");
        registry.addViewController("main.html").setViewName("/main");
        registry.addViewController("icons.html").setViewName("/system/common/icons");
        registry.addViewController("css.html").setViewName("/system/common/css");
        registry.addViewController("footer.html").setViewName("/system/common/footer");
        registry.addViewController("register.html").setViewName("/system/register");
        registry.addViewController("home/login.html").setViewName("/home/login");
        registry.addViewController("home/register.html").setViewName("/home/register");
        registry.addViewController("list.html").setViewName("/index");
    }

    //拦截器相关配置
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //addInterceptor:配置执行哪个拦截器
        //addPathPatterns:配置哪些资源的时候，执行拦截器
        //excludePathPatterns：配置排除资源，访问配置的路径就直接放行，不经过拦截器方法处理
        registry.addInterceptor(new SystemAuthorityInterceptor())
                .addPathPatterns("/system/**")
                .excludePathPatterns(
                        "/login.html",
                        "/system/login",
                        "/header.html",
                        "/left-menu.html",
                        "/footer.html",
                        "/header-menu.html",
                        "/icons.html",
                        "/third-menu.html",
                        "/list.html",
                        "/css/**",
                        "/fonts/**",
                        "/images/**",
                        "/js/**",
                        "/admin/view/**",
                        "/error",
                        "/upload",
                        "/register.html",
                        "/system/register"
                );
    }


}
